package yxy.game.pm2.command.stairway;

import yxy.cherry.data.bean.sportsdate;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.StairwayMessage;
import yxy.game.pm2.module.RankWorshipHandle;
import yxy.game.pm2.module.stairway.RankStairwayKing;
import yxy.game.pm2.module.stairway.StairwayHandle;

/** 天梯争雄 天梯排行榜膜拜 */
@MessageType(proto = Command.StairwayRankWorship, classtype = StairwayMessage.StairwayRankWorshipRequest.class)
public class CMD_StairwayRankWorship extends Command<StairwayMessage.StairwayRankWorshipRequest> {

	@Override
	public void execute(MessageSource messageSource, StairwayMessage.StairwayRankWorshipRequest request) {
		String playerCode = messageSource.getTag();
		int ranking = request.getRank();

		PlayerBean playerBean = PlayerBean.get(playerCode);
		ServerBean serverBean = ServerBean.get(playerBean.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());

		StairwayMessage.StairwayRankWorshipResponse.Builder response = StairwayMessage.StairwayRankWorshipResponse.newBuilder();

		RankStairwayKing rank = new RankStairwayKing(areaBean.getGroupId());
		RankWorshipHandle.RankWorshipEntry worshipEntry = new RankWorshipHandle.RankWorshipEntry();

		sportsdate data = StairwayHandle.data();
		Currencies rewards = new Currencies();
		rewards.add(Currency.create(data.getWorshipgettype(), data.getWorshipgetid(), data.getWorshipgetnum()));

		Result result = new RankWorshipHandle().worship(playerCode, ranking, rank, rewards, worshipEntry);
		if (!result.equals(Result.Success)) {
			send(messageSource, response, result);
			return;
		}
		response.setRank(ranking);
		response.setFans(worshipEntry.getFans());
		response.addAllRewards(worshipEntry.getRewards());
		response.setTimes(worshipEntry.getTimes());

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, StairwayMessage.StairwayRankWorshipResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}